package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetMealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/setmeal")
@Api(tags = "套餐相关接口")
@Slf4j
public class SetMealController {

    @Autowired
    SetMealService mealService;

    @PostMapping
    @ApiOperation("新增套餐")
    @CacheEvict(cacheNames = "setmeal", key = "#setmealDTO.categoryId")
    public Result saveMeal(@RequestBody SetmealDTO setmealDTO){
        mealService.saveSetMeal(setmealDTO);
        return Result.success();
    }

    @GetMapping("/page")
    @ApiOperation("分页查询套餐")
    public Result<PageResult> page (SetmealPageQueryDTO setmealPageQueryDTO){
        return Result.success(mealService.getPage(setmealPageQueryDTO));
    }

    @DeleteMapping
    @ApiOperation("批量删除套餐")
    @CacheEvict(cacheNames = "setmeal", allEntries = true)
    public Result deleteMeal(String ids){
        mealService.batchDeleteMeal(ids);
        return Result.success();
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询套餐")
    public Result<SetmealVO> getMealById(@PathVariable Long id){
        return Result.success(mealService.getSetMealById(id));
    }

    @PutMapping
    @ApiOperation("修改套餐")
    @CacheEvict(cacheNames = "setmeal", key = "#setmealDTO.categoryId")
    public Result updateMeal(@RequestBody SetmealDTO setmealDTO){
        mealService.updateSetMeal(setmealDTO);
        return Result.success();
    }

    @PostMapping("/status/{status}")
    @ApiOperation("停售/起售套餐")
    @CacheEvict(cacheNames = "setmeal", allEntries = true)
    public Result updateMealStatus(@PathVariable Integer status, Long id){
        mealService.startOrStop(id,status);
        return Result.success();
    }
}
